package com.new2i.util;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import com.new2i.model.DoctorEntity;

/**
 * HttpSession管理类
 * 
 * @since 2014/10/20
 * @author 2i
 *
 */
public class HttpSessionUtil {

	/**
	 * 从Session中取出医生信息(Object)
	 * 
	 * @since 2014/10/20
	 * @author 2i
	 * @param request
	 * @return
	 */
	public static DoctorEntity getDoctorFromSession(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("doctor");
		if (obj != null) {
			DoctorEntity doctor = (DoctorEntity) obj;
			return doctor;
		}
		return null;
	}

	/**
	 * 从Session中取出医生信息(HashMap)
	 * 
	 * @since 2014/10/20
	 * @author 2i
	 * @param request
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static HashMap<String, Object> getHashMapFromSession(HttpServletRequest request) {
		Object obj = request.getSession().getAttribute("doctor_map");
		if (obj != null) {
			return (HashMap<String, Object>) obj;
		}
		return null;
	}

	/**
	 * 保存医生信息到Session
	 * 
	 * @param request
	 * @param sHashMap
	 * @return
	 */
	public static DoctorEntity saveDoctorToSession(HttpServletRequest request, HashMap<String, Object> sHashMap) {
		DoctorEntity doctor = new DoctorEntity();
		if (sHashMap != null) {
			// 1医生基本信息
			Integer doctor_id = (Integer)sHashMap.get("doctor_id");
			String doctor_name = (String)sHashMap.get("doctor_name");
			String doctor_photourl = (String)sHashMap.get
					("doctor_photourl");
			doctor.setDoctor_id(doctor_id);
			doctor.setDoctor_name(doctor_name);
			doctor.setDoctor_photourl(doctor_photourl);

			// 3.医院信息
			Object sHospital_id = sHashMap.get("hospital_id");
			Object sHospital_name = sHashMap.get("hospital_name");
			doctor.setHospital_id(Integer.parseInt(sHospital_id.toString()));
			doctor.setHospital_name(sHospital_name == null ? "" : sHospital_name.toString());
			// 4.科室信息
			Object sKeshi_id = sHashMap.get("keshi_id");
			Object sKeshi_name = sHashMap.get("keshi_name");
			doctor.setKeshi_id(Integer.parseInt(sKeshi_id.toString()));
			doctor.setKeshi_name(sKeshi_name == null ? "" : sKeshi_name.toString());

			// token
			Object token = sHashMap.get("token");
			System.out.println("token ==> " + token);
			if (doctor.getDoctor_id() <= 10012) {
				doctor.setToken(token == null ? "" : token.toString());
				System.out.println("after token ==> " + token);
			}
			request.getSession().setAttribute("doctor_map", sHashMap);
			request.getSession().setAttribute("doctor", doctor);
			return doctor;

		}
		return null;

	}

	/**
	 * 注销登录
	 * 
	 * @since 2015/09/01
	 * @author 2i
	 * @param request
	 */
	public static void logoff(HttpServletRequest request) {
		request.getSession().invalidate();
	}

}
